package com.kalaqia.ali.pay.di;

import android.app.Activity;

import com.alipay.sdk.app.PayTask;
import com.kalaqia.ali.pay.PayResult;

import java.util.Map;

import dagger.Module;
import dagger.Provides;
import dagger.Subcomponent;
import io.reactivex.Observable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.annotations.NonNull;
import io.reactivex.functions.BiFunction;
import io.reactivex.functions.Function;
import io.reactivex.schedulers.Schedulers;


/**
 * function:
 * project:kalaqia
 * describe:
 * created owner:kalaqia 421407393@qq.com
 * created date:2017/8/15 14:31
 */
//<editor-fold desc="version update log">
// updare owner:kalaqia  421407393@qq.com
// update datetime:2017/8/15 14:31 
// update remark:
//</editor-fold>
@Module
public class AliPayModule {

    private Activity activity;

    public AliPayModule(Activity activity) {
        this.activity = activity;
    }

    @Provides
    public Observable<PayTask> providePayTaskObservable() {
        return Observable.just(new PayTask(this.activity))
                .observeOn(Schedulers.io());
    }

    @Provides
    public Observable<Map<String, String>> providePayMapObservable(Observable<PayTask> payTaskObservable, Observable<String> payInfoObservable) {
        return payTaskObservable.zipWith(payInfoObservable, new BiFunction<PayTask, String, Map<String, String>>() {
            @Override
            public Map<String, String> apply(@NonNull PayTask payTask, @NonNull String orderInfo) throws Exception {
                return payTask.payV2(orderInfo, true);
            }
        }).observeOn(Schedulers.io());
    }

    @Provides
    public Observable<PayResult> providePayResultObservable(Observable<Map<String, String>> payMapObservable) {
        return payMapObservable.map(new Function<Map<String, String>, PayResult>() {
            @Override
            public PayResult apply(@NonNull Map<String, String> map) throws Exception {
                return new PayResult(map);
            }
        }).observeOn(AndroidSchedulers.mainThread());
    }

}
